﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Object;
using Model.DataContext;

namespace Nongsan.Controllers
{
    public class SanPhamController : Controller
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /SanPham/
       public ActionResult jsonList()
        {


            var list = from n in db.SanPhams select new { n.ID, n.Ten, n.Chi_tiet, n.Gia, n.So_luong, n.Don_vi_tinh, n.LoaiSanPham.Ten_Loai};
			
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {

            return View();

        }

        //
        // GET: /SanPham/Details/5

        public ActionResult Details(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            return View(sanpham);
        }

        //
        // GET: /SanPham/Create

        public ActionResult Create()
        {
            ViewBag.ID_Loaisanpham = new SelectList(db.LoaiSanPhams, "ID", "Ten_Loai");
            return View();
        }

        //
        // POST: /SanPham/Create
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(SanPham sanpham)
        {
            if (ModelState.IsValid)
            {
                //sanpham.Chi_tiet = HttpUtility.HtmlEncode(sanpham.Chi_tiet);
                db.SanPhams.Add(sanpham);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ID_Loaisanpham = new SelectList(db.LoaiSanPhams, "ID", "Ten_Loai", sanpham.ID_Loaisanpham);
            return View(sanpham);
        }

        //
        // GET: /SanPham/Edit/5

        public ActionResult Edit(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID_Loaisanpham = new SelectList(db.LoaiSanPhams, "ID", "Ten_Loai", sanpham.ID_Loaisanpham);
            return View(sanpham);
        }

        //
        // POST: /SanPham/Edit/5
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(SanPham sanpham)
        {
            if (ModelState.IsValid)
            {
                //sanpham.Chi_tiet = HttpUtility.HtmlEncode(sanpham.Chi_tiet);
                db.Entry(sanpham).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ID_Loaisanpham = new SelectList(db.LoaiSanPhams, "ID", "Ten_Loai", sanpham.ID_Loaisanpham);
            return View(sanpham);
        }

        //
        // GET: /SanPham/Delete/5

        public ActionResult Delete(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            return View(sanpham);
        }

        //
        // POST: /SanPham/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            db.SanPhams.Remove(sanpham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
		[HttpPost]
        public ActionResult ajDelete(int id)
        {
             SanPham sanpham = db.SanPhams.Find(id);
			 if (sanpham == null)
            {
                return Json(new { success = false ,msg="Không tìm thấy đối tượng"});
            }

         
            db.SanPhams.Remove(sanpham);
			db.SaveChanges();
            return Json(new { success=true});
        }
		
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}